<template>
	<div class="system-user-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="userDialogFormRef" :rules="rules" :model="state.ruleForm" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="账户名称" prop="username">
							<el-input v-model="state.ruleForm.username" placeholder="请输入账户名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="账户密码" :prop="state.dialog.type === 'edit'?'':'password'">
							<el-input v-model="state.ruleForm.password" placeholder="请输入" type="password"
								clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="用户状态" prop="status">
							<el-switch v-model="state.ruleForm.status" inline-prompt active-text="启"
								inactive-text="禁"></el-switch>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="systemUserDialog">
import { reactive, ref, nextTick } from 'vue';
import { useSystemApi } from "/@/api/system";

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

const systemApi = useSystemApi();

// 定义变量内容
const userDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		id: '',
		username: '', // 账户名称
		password: '', // 账户密码
		status: true, // 用户状态
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

const rules = reactive({
	username: [
		{ required: true, message: '用户名不能为空', trigger: 'blur' },
	],
	password: [
	{ required: true, message: '密码不能为空', trigger: 'blur' },
	],
})

// 打开弹窗
const openDialog = (type, id) => {
	state.dialog.type = type;
	if (type === 'edit') {
		getUserInfo(id);

		state.dialog.title = '修改用户';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增用户';
		state.dialog.submitTxt = '新 增';
		// 清空表单，此项需加表单验证才能使用
		nextTick(() => {
			state.dialog.params.password = '';
			state.dialog.params.status = true;
			userDialogFormRef.value.resetFields();
	
		});
	}
	state.dialog.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};

// 获取用户信息
const getUserInfo = (id) => {
	systemApi.userInfo(id).then(res => {
		console.log(res)
		state.ruleForm.id = res.data.id;
		state.ruleForm.status = res.data.status;
		state.ruleForm.username = res.data.username;
		state.ruleForm.password = '';
	})
};


// 提交
const onSubmit = () => {
	if (!userDialogFormRef.value) return

	userDialogFormRef.value.validate((valid) => {
		if (valid) {
			if (state.dialog.type === 'edit') {
				console.log(state.ruleForm.status)
				let params = {
					"id": state.ruleForm.id,
					// "username": state.ruleForm.username,
					"password": state.ruleForm.password,
					"status": state.ruleForm.status,
				}
				systemApi.userUpdate(params).then(() => {
					closeDialog();
					emit("refresh")
				})
			} else {
				let params = {
					"username": state.ruleForm.username,
					"password": state.ruleForm.password,
					"status": state.ruleForm.status,
				}
				systemApi.userAdd(params).then(() => {
					closeDialog();
					emit("refresh")
				})
			}
		}
	})
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
